package com.icss.ah.activity.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class UseStatisticService {
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	
	/**
	 * 优惠卷列表查询
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getQueryList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("UseStatisticBean.getQueryList",map);
			for(int i=0;i<list.size();i++){
				String countId = list.get(i).get("countId").toString();
				int count = Integer.parseInt(countId);
				double money = 0;
				if(count > 0){
					 String moneyflag =  list.get(i).get("money").toString();
					 double singleMoney = Double.parseDouble(moneyflag);
					 money = singleMoney * count;
				}else{
				}
				list.get(i).put("money", money);
				list.get(i).put("countId", countId);
			}
			infolog.info("UseStatisticService|getQueryList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("UseStatisticService|getQueryList", e);
		}
		return list;
	}
	
	/**
	 * 优惠卷统计
	 * @param map
	 * @return
	 */
	public Long getQueryListCount(Map<String, String> map){
		Long count = 0L;
		try {
			count = sqlSessionTemplate.selectOne("UseStatisticBean.getQueryListCount",map);
			infolog.info("UseStatisticService|getQueryListCount|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("UseStatisticService|getQueryListCount", e);
		}
		return count;
	}
	
	/**
	 * 使用优惠券用户
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> getUserDetail(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("UseStatisticBean.getUserDetail",map);
			infolog.info("UseStatisticService|getUserDetail");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("UseStatisticService|getUserDetail", e);
		}
		return list;
	}
	
	/**
	 * 使用优惠券用户数量
	 * @param map
	 * @return
	 */
	public Long getUserDetailCount(Map<String, String> map){
		Long count = 0L;
		try {
			count = sqlSessionTemplate.selectOne("UseStatisticBean.getUserDetailCount",map);
			infolog.info("UseStatisticService|getUserDetailCount|count: "+count);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("UseStatisticService|getUserDetailCount", e);
		}
		return count;
	}
}
